package me.zingon.self.bigdispatcher.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.zingon.self.bigdispatcher.constant.JobDataKey;
import me.zingon.self.bigdispatcher.dto.R;
import me.zingon.self.bigdispatcher.entity.NacosJob;
import me.zingon.self.bigdispatcher.job.NacosJobDetail;
import me.zingon.self.bigdispatcher.service.NacosJobService;
import me.zingon.self.bigdispatcher.util.NacosCaller;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * (NacosJobDetail)表控制层
 *
 * @author zingon
 * @since 2020-12-30 10:35:25
 */
@RestController
@RequestMapping("/nacosJob")
@CrossOrigin(origins = "*",maxAge = 3600)
@Validated
@Slf4j
public class NacosJobController {
    /**
     * 服务对象
     */
    @Autowired
    private NacosJobService nacosJobService;

    @Autowired
    private NacosCaller nacosCaller;

    @Autowired
    Scheduler scheduler;

    @GetMapping("/get")
    public NacosJob getById(@NotNull(message = "not null") Long id){
        return nacosJobService.getById(id);
    }


    @PostMapping("/add")
    public R add(@RequestBody NacosJob job) throws SchedulerException {
        return R.success(nacosJobService.addJob(job));
    }

    @GetMapping("/list")
    public R list() throws SchedulerException {
        return R.success(nacosJobService.list(null));
    }

    @GetMapping("/schedule")
    public R scheduleJob(Long id) throws SchedulerException {
        NacosJob job=nacosJobService.getById(id);
        return R.success(nacosJobService.scheduleJob(job));
    }

    @GetMapping("/unschedule")
    public R unschedule(Long id) throws SchedulerException {
        NacosJob job=nacosJobService.getById(id);
        return R.success(nacosJobService.unschedule(job));
    }




}